﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Data;

using NUnit.Framework;

namespace SmartProject.Testing
{
    [TestFixture]
    public class Test09_Sprints
    {
        [TestFixtureSetUp()]
        public void Start()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    BusinessRulePreferences.Username username =
                        new BusinessRulePreferences.Username();
                    username.SetSelectedBusinessRule(session, "Simple username");
                    User.NewUser(session, "David", "Lee", "dlee", "dlee@test.com", "Any", new string[] { "User" });
                    transaction.Commit();
                    transaction.Dispose();
                }
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test01_AddSprint()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    Assert.IsNull(Sprint.GetSprint(session, -1));

                    Sprint sprint1 = new Sprint();
                    sprint1.Description = "Start";
                    sprint1.EstimatedDays = 7;
                    sprint1.Scheduled = DateTime.Now.AddDays(5);
                    sprint1.Owner = User.GetUserByUsername(session, "dlee");
                    sprint1.Save(session);

                    transaction.Commit();
                    transaction.Dispose();
                }
                IList<Sprint> sprints = Sprint.GetSprints(session, SprintType.All);
                Assert.AreEqual(1, sprints.Count);

                Sprint sprint = sprints[0];
                Assert.AreEqual("Start", sprint.Description);
                Assert.AreEqual(7, sprint.EstimatedDays);
                Assert.AreEqual("dlee", sprint.Owner.Username);
            }
        }

        [Test]
        public void Test02_UpdateSprint()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    Assert.IsNull(Sprint.GetSprint(session, -1));

                    IList<Sprint> sprints = Sprint.GetSprints(session, SprintType.All);
                    Sprint sprint = sprints[0];
                    int id = sprint.Id;
                    sprint.Description = "Some Description";
                    sprint.EstimatedDays = 10;
                    sprint.Scheduled = DateTime.Now;
                    sprint.Started = DateTime.Now;
                    sprint.Save(session);

                    sprint = Sprint.GetSprint(session, id);
                    Assert.AreEqual("Some Description", sprint.Description);
                    Assert.AreEqual(10, sprint.EstimatedDays);
                    transaction.Commit();
                    transaction.Dispose();
                }
            }
        }

        [Test]
        public void Test03_DeleteSprint()
        {
            using (var session = DataProvider.Instance.Session)
            {
                IList<Sprint> sprints;
                using (var transaction = session.BeginTransaction())
                {
                    sprints = Sprint.GetSprints(session, SprintType.All);
                    for (int i = 0; i < sprints.Count; i++)
                    {
                        sprints[i].Remove(session);
                    }
                    Sprint.Clean(session);
                    transaction.Commit();
                }
                sprints = Sprint.GetSprints(session, SprintType.All);
                Assert.AreEqual(0, sprints.Count);
            }
        }

        [TestFixtureTearDown]
        public void End()
        {
            using (var session = DataProvider.Instance.Session)
            {
                User user = User.GetUserByUsername(session, "dlee");
                using (var transaction = session.BeginTransaction())
                {
                    user.Remove(session);
                    User.Clean(session);
                    transaction.Commit();
                    transaction.Dispose();
                }
                session.Close();
                session.Dispose();
            }
        }
    }
}
